package com.sykj.bsc.cl.service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sykj.bsc.cl.bean.WechatUser;
import com.sykj.bsc.util.FileUtil;
import com.sykj.bsc.util.Tools;
import com.sykj.bsc.zjj.bean.Bis_custom_user;
import com.sykj.bsc.zjj.mapper.BisCustomUserMapper;

@Service
public class WechatCommonService {

	@Autowired
	private BisCustomUserMapper userMapper;
	public Bis_custom_user saveCustomInfo(WechatUser user,String rootPath){
		Map<String,Object> map=new HashMap<String,Object>();
		System.out.println(user.getOpenid());
		map.put("openid",user.getOpenid());
		map.put("is_del","0");
		List<Bis_custom_user> list=userMapper.listAll(map);
		Bis_custom_user customUser=null;
		int flag=0;
		if(list.size()>0){
			customUser=list.get(0);
			flag=1;
		}else{
			customUser=new Bis_custom_user();
			customUser.setCustom_user_id(UUID.randomUUID().toString());
			flag=0;
		}
		/*String url=user.getHeadimgurl();
		if(Tools.isNotNull(url)){
			String fileName=customUser.getCustom_user_id()+".png";
			String folderPath = rootPath+File.separator+"/imageFile/customImage/";
			FileUtil.createFolder(folderPath);
			String saveFilePath=folderPath+File.separator+fileName;
			FileUtil.downloadUrlFile(url,saveFilePath);//下载头像信息
			customUser.setPhoto("/imageFile/customImage/"+fileName); //存储用户照片URL地址
		}*/
		customUser.setPhoto(user.getHeadimgurl()); //存储用户照片URL地址
		customUser.setOpenid(user.getOpenid());
		customUser.setNikename(user.getNickname());
		customUser.setSex(user.getSex()+"");
		customUser.setRemark(user.getRemark());
		customUser.setAddress(user.getProvince()+user.getCity());
		customUser.setEnroll_mode("1");//微信注册
		customUser.setEnroll_time(new Date());
		customUser.setWechat_status("2");//已订阅
		if(flag==0){
			userMapper.saveCustomUser(customUser);
		}else{
			userMapper.updateCustomUser(customUser);
		}	
		System.out.println("微信授权成功");
		return customUser;
		}
	
	public void updateCustomUser(Bis_custom_user user){
		this.userMapper.updateCustomUser(user);
	}
}
